﻿Type.registerNamespace('Html5VideoTabWebPart');

//global vars
var _webPartPageComponentId;
var _isVideoPlaying = false;
var _isVideoMuted = false;
var _playNotifyId;
var _statusTimeId;
var _statusErrorId;
var _endedNotifyId;

Html5VideoTabWebPart.CustomPageComponent = function Html5VideoTabWebPart_CustomPageComponent(webPartPcId) {
    this._webPartPageComponentId = webPartPcId;
    Html5VideoTabWebPart.CustomPageComponent.initializeBase(this);
}

Html5VideoTabWebPart.CustomPageComponent.prototype = {
    init: function Html5VideoTabWebPart_CustomPageComponent$init() { },

    getFocusedCommands: function Html5VideoTabWebPart_CustomPageComponent$getFocusedCommands() {
        return ['Html5VideoTab.EnableCustomTab', 'Html5VideoTab.EnableCustomGroup', 'Html5VideoTab.Play', 'Html5VideoTab.Stop', 'Html5VideoTab.Pause', 'Html5VideoTab.Mute', 'Html5VideoTab.VolumeUp', 'Html5VideoTab.VolumeDown'];
    },

    getGlobalCommands: function Html5VideoTabWebPart_CustomPageComponent$getGlobalCommands() {
        return [];
    },

    isFocusable: function Html5VideoTabWebPart_CustomPageComponent$isFocusable() {
        return true;
    },

    canHandleCommand: function Html5VideoTabWebPart_CustomPageComponent$canHandleCommand(commandId) {
        if (_isVideoPlaying) {
            if (commandId == 'Html5VideoTab.Play') { return false; }
            if ((commandId == 'Html5VideoTab.Pause') || (commandId == 'Html5VideoTab.Stop')) { return true; }
        }
        else {
            if (commandId == 'Html5VideoTab.Play') { return true; }
            if ((commandId == 'Html5VideoTab.Pause') || (commandId == 'Html5VideoTab.Stop')) { return false; }
        }

        if (_isVideoMuted) {
            if ((commandId === 'Html5VideoTab.VolumeUp') || (commandId === 'Html5VideoTab.VolumeDown')) { return false; }
        }
        else {
            if ((commandId === 'Html5VideoTab.VolumeUp') || (commandId === 'Html5VideoTab.VolumeDown')) { return true; }
        }

        if ((commandId === 'Html5VideoTab.EnableCustomTab') || (commandId === 'Html5VideoTab.EnableCustomGroup') || (commandId === 'Html5VideoTab.Mute')) {
            return true;
        }
    },

    handleCommand: function Html5VideoTabWebPart_CustomPageComponent$handleCommand(commandId, properties, sequence) {

        var video = document.getElementById(this.getVideoId());

        if (commandId == 'Html5VideoTab.Play') {

            SP.UI.Notify.removeNotification(_endedNotifyId);

            //play video
            if (video.paused) {

                //timer on status bar
                SP.UI.Status.removeStatus(_statusTimeId);
                _statusTimeId = SP.UI.Status.addStatus('VIDEO TIME:', '', true);

                video.play();
                _playNotifyId = SP.UI.Notify.addNotification("Playing video...");
                _isVideoPlaying = true;
            }
            else { //handle video just paused
                //_playNotifyId = SP.UI.Notify.addNotification("Video is still playing...");
            }

            //add 'ended' event listener
            video.addEventListener('ended', function () {
                _isVideoPlaying = false;
                video.currentTime = 0;
                video.pause();

                SP.UI.Status.removeStatus(_statusTimeId);

                SP.UI.Notify.removeNotification(_playNotifyId);
                SP.UI.Notify.removeNotification(_endedNotifyId);
                _endedNotifyId = SP.UI.Notify.addNotification("Video ended !", false);

                //refersh the ribbon bar
                RefreshCommandUI();
            });

            //add 'error' event listener
            video.addEventListener('error', function (e) {
                _statusErrorId = SP.UI.Status.addStatus("ERROR", "Error in loading video!", true);
                SP.UI.Status.setStatusPriColor(_statusErrorId, 'Red');
            }, true);

            //add 'timeupdate' event lister
            video.addEventListener('timeupdate', function (e) {
                var s = e.target.currentTime;
                var h = Math.floor(s / 3600);
                s = s % 3600;
                var m = Math.floor(s / 60);
                s = Math.floor(s % 60);
                if (s.toString().length < 2) s = "0" + s;
                if (m.toString().length < 2) m = "0" + m;

                SP.UI.Status.updateStatus(_statusTimeId, h + ":" + m + ":" + s);
            }, true);
        }
        else if (commandId === 'Html5VideoTab.Pause') {
            //pause the video
            if (!video.paused) {                
                video.pause();
                _isVideoPlaying = false;
                SP.UI.Notify.removeNotification(_playNotifyId);
                _playNotifyId = SP.UI.Notify.addNotification("Video paused!");
            }
        }
        else if (commandId == 'Html5VideoTab.Stop') {
            //pause the video and go back to the first frame
            if (!video.paused) {                
                video.pause();
                _isVideoPlaying = false;
                SP.UI.Notify.removeNotification(_playNotifyId);
                _playNotifyId = SP.UI.Notify.addNotification("Video stopped!");
            }
            video.currentTime = 0;
        }
        else if (commandId == 'Html5VideoTab.VolumeDown') {
            video.muted = false;
            if (video.volume > 0) {
                video.volume -= 0.1;
                if (video.volume <= 0) {
                    video.muted = true;
                }
            }
        }
        else if (commandId == 'Html5VideoTab.VolumeUp') {
            video.muted = false;
            if (video.volume >= 0 && video.volume < 1) {
                video.volume += 0.1;
            }
        }
        else if (commandId == 'Html5VideoTab.Mute') {
            if (properties.On) {
                _isVideoMuted = true;
                video.muted = true;
            }
            else {
                _isVideoMuted = false;
                video.muted = true;
            }
        }

        //refersh the ribbon bar
        RefreshCommandUI();
    },

    getId: function Html5VideoTabWebPart_CustomPageComponent$getId() {
        return this._webPartPageComponentId;
    },

    getVideoId: function Html5VideoTabWebPart_CustomPageComponent$getVideoId() {
        var videoId = this.getId() + "_video";
        if (videoId.indexOf("WebPart") >= 0)
            videoId = videoId.replace("WebPart", "");
        return videoId;
    }
}


Html5VideoTabWebPart.CustomPageComponent.registerClass('Html5VideoTabWebPart.CustomPageComponent', CUI.Page.PageComponent);
SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("Html5VideoTabPageComponent.js");